import { Controller } from "../../mvc/core/Controller";
import { DataEvent } from "../events/DataEvent";
import { ItemData } from "../resource/info/ItemData";
import { GameProxy } from "../mvc/GameProxy";
import { MsgManager } from "../manager/MsgManager";
import { Language } from "../../../extend/language/Language";
import { MessageBlock } from "../../../Framework/MQ/MessageBlock";
import { SAttribute } from "../../../Message/Public/SAttribute";
import { EHandInMaterialType } from "../../../Message/Game/EHandInMaterialType";
import { IView } from "../../mvc/interfaces/IView";
import { HandinMaterialModule } from "../../../modules/HandinMaterialModule";
import { NetDispatcher } from "../../mvc/core/NetDispatcher";
import { ServerCommand } from "../mvc/ServerCommand";
import { Dispatcher } from "../../mvc/core/Dispatcher";
import { EventName } from "../mvc/EventName";
import { EGateCommand } from "../../../Message/Command/EGateCommand";
import { ClockManager } from "../manager/ClockManager";
import { TimeEvent } from "../events/TimeEvent";
import { IHandinMaterialModule } from "../../../modules/interfaces/IHandinMaterialModule";
type int = number;
//class HandinMaterialController
    
    export  class HandinMaterialController extends Controller
    {
       

        /* internal  */onShowHandinMaterialFixStone(arg1: DataEvent): void
        {
            this.handinMaterialModule.showHandinMaterialFixStone();
            this.handinMaterialModule.updateMaterials();
            this.onGetFixStoneMaterialNum(null);
            return;
        }

        /* internal  */onShowHandinMaterialMakeCake(arg1: DataEvent): void
        {
            this.handinMaterialModule.showHandinMaterialMakeCake();
            this.handinMaterialModule.updateMaterials();
            this.onGetCakeMaterialNum(null);
            return;
        }

        /* internal  */doHandinMaterial(arg1: DataEvent): void
        {
            var loc6=null;
            var loc1=arg1.data.item as ItemData;
            var loc2=arg1.data.type as int;
            var loc3=new Map<any, any> /* flash.utils.Dictionary */();
            var loc4=false;
            var loc5;
            (loc5 = cache.pack.backPackCache.getPropItems()).sortOn("bind", Array.DESCENDING);
            var loc7=0;
            var loc8=loc5;
            for(loc6 of loc8) 
            {
                if (!(loc6.itemCode == loc1.itemCode || loc6.itemCode == loc1.itemInfo.item.codeUnbind)) 
                {
                    continue;
                }
                loc3[loc6.uid] = 1;
                loc4 = true;
                break;
            }
            if (loc4) 
            {
                GameProxy.handinMaterial.handInMaterial(loc3, loc2);
            }
            else 
            {
                MsgManager.showRollTipsMsg(Language.getString(69804));
            }
            return;
        }

        /* internal  */onBackpackDataChange(arg1): void
        {
            if (this._handinMaterialModule) 
            {
                this._handinMaterialModule.updateMaterials();
            }
            return;
        }

        /* internal  */onGetCakeMaterialNum(arg1: MessageBlock): void
        {
            var loc1=null;
            if (arg1) 
            {
                loc1 = arg1.messageBase as SAttribute;
                this._cakeMaterialNum = loc1.value;
            }
            if (this._handinMaterialModule) 
            {
                this.handinMaterialModule.updateHandinMaterialNum(EHandInMaterialType._EHandInMaterialMakeCake, this._cakeMaterialNum);
            }
            return;
        }

        /* internal  */onGetFixStoneMaterialNum(arg1: MessageBlock): void
        {
            var loc1=null;
            if (arg1) 
            {
                loc1 = arg1.messageBase as SAttribute;
                this._fixStoneMaterialNum = loc1.value;
            }
            if (this._handinMaterialModule) 
            {
                this.handinMaterialModule.updateHandinMaterialNum(EHandInMaterialType._EHandInMaterialFixStone, this._fixStoneMaterialNum);
            }
            return;
        }

        /* internal  */onDateChange(arg1): void
        {
            this._cakeMaterialNum = 0;
            this._fixStoneMaterialNum = 0;
            this.onGetCakeMaterialNum(null);
            this.onGetFixStoneMaterialNum(null);
            return;
        }

        protected /* override */ initView(): IView
        {
            if (!this._handinMaterialModule) 
            {
                this._handinMaterialModule = new HandinMaterialModule();
                NetDispatcher.addCmdListener(ServerCommand.BackPackItemsChange, this.onBackpackDataChange);
            }
            return this._handinMaterialModule;
        }

        protected /* override */ initServer(): void
        {
            Dispatcher.addEventListener(EventName.ShowHandinMaterialFixStone, this.onShowHandinMaterialFixStone);
            Dispatcher.addEventListener(EventName.ShowHandinMaterialMakeCake, this.onShowHandinMaterialMakeCake);
            Dispatcher.addEventListener(EventName.HandinMaterial, this.doHandinMaterial);
            NetDispatcher.addCmdListener(EGateCommand._ECmdGateCakeMaterialNum, this.onGetCakeMaterialNum);
            NetDispatcher.addCmdListener(EGateCommand._ECmdGateStoneMaterialNum, this.onGetFixStoneMaterialNum);
            ClockManager.instance.addEventListener(TimeEvent.DateChange, this.onDateChange);
            return;
        }

        public get handinMaterialModule(): IHandinMaterialModule
        {
            if (!this._handinMaterialModule) 
            {
                this.initView();
            }
            return this._handinMaterialModule;
        }

        private /* var */_handinMaterialModule: IHandinMaterialModule;

        private /* var */_cakeMaterialNum: int;

        private /* var */_fixStoneMaterialNum: int;
    }
